package common.action;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import static common.utils.Constants.*;

public class PermitFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");//不加其他会乱码
		response.setCharacterEncoding("utf-8");
		HttpServletRequest rq = (HttpServletRequest) request;
		HttpServletResponse rp = (HttpServletResponse) response;
		String path = rq.getServletPath();
		if (path.indexOf("delete")==-1) {
			filterChain.doFilter(request, response);
		} else {
			HttpSession session=rq.getSession();
			int permit=(Integer) session.getAttribute(PERMIT);
			if(permit==1){
				filterChain.doFilter(request, response);
			}else{
				StringBuffer contextUrl = rq.getRequestURL();
				StringBuffer basePath = contextUrl.delete(
						contextUrl.length() - rq.getRequestURI().length(),
						contextUrl.length()).append(rq.getContextPath())
						.append("/");
				rp.sendRedirect(basePath + "panel/nopower.jsp");
				
			}

		}
	/**
	 * 	if (path.indexOf("findById")==-1){
	
			filterChain.doFilter(request, response);
		}else{
			HttpSession session=rq.getSession();
			int adminId=
			int id=(Integer) session.getAttribute(ADMIN_ID);
			if(id=)
		} */
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}
	
}
